package com.ping.controller;

import java.util.LinkedList;
import java.util.List;

import com.eaio.uuid.UUID;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.aop.ClearLayer;
import com.jfinal.plugin.activerecord.Record;
import com.ping.model.ResponseEnum;
import com.ping.model.User;
import com.ping.service.BaseService;

public class UserController extends BaseController{
	
	@ClearInterceptor(ClearLayer.ALL)
	public void add(){
		if (!checkQuery()) {
			return;
		}
		
		String query = getPara(QUERY_STATEMENT);
		String[] necessary = {User.NAME, User.PHONE, User.PWD};
		
 		BaseService service = new BaseService() {
			@Override
			public void genRecord(Record record, LinkedList<String> params,
					LinkedList<String> values) {
				// TODO Auto-generated method stub
				params.add(User.UUID);
				UUID uuid = new UUID();
				values.add(uuid.toString());
				
				super.genRecord(record, params, values);
			}
		};
		
		if(!service.meet(query, necessary)){
			renderLack();
			return;
		}
		
		if(service.add(User.TABLE_NAME, query)){
			System.out.println("Add Successfully");
			renderSuccess();
		}else {
			renderOperateError();
		}
	}
	
	@ClearInterceptor(ClearLayer.ALL)
	public void login(){
		if (!checkQuery()) {
			return ;
		}
		
		String query = getPara(QUERY_STATEMENT);
		String[] params = {User.PHONE, User.PWD};
		BaseService service = new BaseService() {};
		
		List<Record> records = service.find(query, User.SQL_GET_BY_NAME7PWD, params);
		
		if (records == null) {
			renderOperateError();
		}else if (records.size() == 0) {
			renderNullResult();
		}else {
			renderResult(records, false);
		}
	}
}
